from HTMLParser import HTMLParser
from urllib import urlopen, urlretrieve
import re
import logging

course_num = 151
url =  "http://showmedovideos4.com/ShowMeDos/"
save_path = r'D:\Downloads\showmedo'
file_regexp = re.compile(str(course_num) + r'[0-9]{4}\.flv')

class FileFinder(HTMLParser):
      li = False
      _files = []
      def __init__(self, url):
            HTMLParser.__init__(self)
            req = urlopen(url)
            self.feed(req.read())
      def handle_starttag(self, tag, attrs):
            attrs = dict(attrs)
            if tag == 'li': self.li = True
            if self.li and tag == 'a' and 'href' in attrs:
                  if re.match(file_regexp, attrs['href']):
                        self.files.append(attrs['href'])
                  #else: print "rejected :" + attrs['href']
      def handle_endtag(self,tags):
            if tags == 'li': self.li != self.li
      @property
      def files(self):
            return self._files

def _reporthook(numblocks, blocksize, filesize):
      try:
            percent = min((numblocks*blocksize*100)/filesize, 100)
            print "Filesize %r Blocksize %r Numblocks %r PCT: %r" % (filesize,blocksize,numblocks,percent)            
      except:
            print "ERROR: Filesize %r Blocksize %r Numblocks %r" % (filesize,blocksize,numblocks)
      if numblocks != 0:
            print str(percent)+'%'
   

file_find = FileFinder(url)
for i in file_find.files:
      print "starting to load file", i
      urlretrieve(url + '/' + i,save_path+'\\'+i,_reporthook)
      print "done", i

            
                  






